﻿using KIS公有云测试.Models;
using KIS公有云测试.Tools;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace KIS公有云测试.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        [HttpPost]
        public JsonResult Getaaa()
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return1>("https://api.kingdee.com/koas/user/sms_login_vcode?client_id=308386&client_secret=5b6ae64cc15da7f5659e577ef41cad74", dic, "{\"mobile\":\"15663744222\"}");
            //var resquest = authService.AccessUrlByPOST<dynamic>("https://yycs.hcgccloud.com/iuap-ipass-runtime/ApRefund/insert", dic, "{}");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult Getbbb(string code)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/sms_login_access_token?client_id=308386&client_secret=5b6ae64cc15da7f5659e577ef41cad74", dic, "{\"mobile\":\"15663744222\",\"vcode\":\"" + code + "\",\"ccode\":\"86\"}");

            ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }

        /// <summary>
        /// 获取用户账套列表
        /// </summary>
        /// <param name="session_id"></param>
        /// <param name="session_secret"></param>
        /// <param name="access_token"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Getccc(string session_id, string session_secret, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            string Signature = sha256(random, session_id, "98e813fb-9317-cbb1-db8e-a56bff07f9c2", session_secret, timestamp.ToString());
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Signature", Signature);
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/account?access_token=" + access_token, dic, "{\"session_id\":\"" + session_id + "\"}");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult Getddd()
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            string Signature = sha256(random, "se1747302940chsgchyrqwuuzxqnbjto", "98e813fb-9317-cbb1-db8e-a56bff07f9c2", "jogzqvvwrpgbupuxinelltrjbevvlpie", timestamp.ToString());
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Signature", Signature);
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/account_applist?access_token=17387132399d882dd9b9939aaa0354cf", dic, "{\"session_id\":\"se1747302940chsgchyrqwuuzxqnbjto\",\"acctnumber\":\"UE171653082025041608411191R\",\"client_id\":\"308386\",\"pid\":\"1645489349c2cdfddc659bf3bc2bbcdd\"}");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult Geteee(string session_id, string session_secret, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            string Signature = sha256(random, session_id, "98e813fb-9317-cbb1-db8e-a56bff07f9c2", session_secret, timestamp.ToString());
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Signature", Signature);
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/get_service_gateway?access_token=" + access_token, dic, "{\"session_id\":\"" + session_id + "\",\"pid\":\"16909605676ceced13ecda152cccddcd\",\"acctnumber\":\"UE171653082024022911025986S\",\"icrmid\":\"2c9223b083cc0f130183e4c32be01544\"}");
            //原token17387132399d882dd9b9939aaa0354cf
            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = resquest,
                Code = 200
            };

            return Json(result);
        }
        /// <summary>
        /// 获取auth_code
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Getfff(string access_token)
        {
            //17387132399d882dd9b9939aaa0354cf 赵磊的token
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            // 创建一个字典对象用于存储键值对，其中键和值都是字符串类型
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/auth_code?access_token=" + access_token, dic, "");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = resquest,
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult Getggg(string auth_code)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/auth_code_login_access_token?client_id=308386&client_secret=5b6ae64cc15da7f5659e577ef41cad74", dic, "{\"auth_code\":\"" + auth_code + "\"}");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        /// <summary>
        /// 用auth_code获取token和sessionid
        /// </summary>
        /// <param name="auth_code"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetOauth2bycode(string auth_code)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            //https://api.kingdee.com/koas/oauth2/authorize
            var resquest = authService.AccessUrlByPOST<Return2>(" https://api.kingdee.com/koas/user/auth_code_login_access_token?client_id=308386&client_secret=5b6ae64cc15da7f5659e577ef41cad74", dic, "{\"auth_code\":\"" + auth_code + "\"}");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = resquest,
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult Getauth_codebytoken(string token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/auth_code?access_token=" + token + "&redirect_uri=https://www.baidu.com", dic, "");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        /// <summary>
        /// 获取登录状态
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="session_id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetUserLoginstatus(string access_token, string session_id)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            var authService = new AuthService();
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/user/login_status?access_token=" + access_token, dic, "{\"session_id\":\"" + session_id + "\"}");

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }

        /// <summary>
        /// 批量查询职员基础资料详情
        /// </summary>
        /// <param name="authdata"></param>
        /// <param name="access_token"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetempList(string authdata, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            authdata = authdata.Replace("\\/", "/");
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-AuthData", authdata);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            dic.Add("X-GW-Router-Addr", "https://kisyunbj.kingdee.com");
            var authService = new AuthService();
            //var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\",\"ParentId\":\"0\",\"Detail\":\"true\",\"SearchKey\":\"\"}";
            var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\"}";
            //,"SearchKey":"01.51.02","Detail":"true"
            //var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/app007104/api/purchasereceipt/list?access_token=" + access_token, dic, jsonBody);
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/APP006992/api/Material/List?access_token=" + access_token, dic, jsonBody);
            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult GetItemInfo(string authdata, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            authdata = authdata.Replace("\\/", "/");
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-AuthData", authdata);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            dic.Add("X-GW-Router-Addr", "https://kisyunbj.kingdee.com");
            var authService = new AuthService();
            //var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\",\"ParentId\":\"0\",\"Detail\":\"true\",\"SearchKey\":\"\"}";
            var jsonBody = "{\"ItemId\":\"1005\"}";
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/APP006992/api/Material/GetDetail?access_token=" + access_token, dic, jsonBody);

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };
            timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            authdata = authdata.Replace("\\/", "/");
            random = new RandomTool().GenerateRandom();
            dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-AuthData", authdata);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            dic.Add("X-GW-Router-Addr", "https://kisyunbj.kingdee.com");
            authService = new AuthService();
            //var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\",\"ParentId\":\"0\",\"Detail\":\"true\",\"SearchKey\":\"\"}";
            jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"10\"}";
            resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/APP006992/api/StockPlace/List?access_token=" + access_token, dic, jsonBody);
            return Json(result);
        }
        [HttpPost]
        public JsonResult GetMeasureUnit(string authdata, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            authdata = authdata.Replace("\\/", "/");
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-AuthData", authdata);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            dic.Add("X-GW-Router-Addr", "https://kisyunbj.kingdee.com");
            var authService = new AuthService();
            //var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\",\"ParentId\":\"0\",\"Detail\":\"true\",\"SearchKey\":\"\"}";
            var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\"}";
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/APP006992/api/MeasureUnit/List?access_token=" + access_token, dic, jsonBody);

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult Purchasereceipt(string authdata, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            authdata = authdata.Replace("\\/", "/");
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-AuthData", authdata);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            dic.Add("X-GW-Router-Addr", "https://kisyunbj.kingdee.com");
            var authService = new AuthService();
            var jsonBody = "{\"Object\":{\"Head\":{\"Fdate\":\"2025-11-20\",\"FPOStyle\":\"252\",\"FSupplyID\":\"1165\",\"FDCStockID\":\"378\",\"FSManagerID\":\"1339\",\"FFManagerID\":\"1339\",\"FDeptID\":\"1295\",\"FEmpID\":\"1339\",\"FExplanation\":\"泛微推送\"},\"Entry\":[{\"FItemID\":\"8669\",\"FBatchNo\":\"CS1120-3\",\"Famount\":\"143900\",\"FAuxPrice\":\"143900\",\"FDCSPID\":\"0\",\"FDCStockID\":\"378\",\"FUnitID\":\"375\",\"FSecCoefficient\":0,\"FSecQty\":0,\"Fauxqty\":\"1\",\"FPlanMode\":\"14036\"}]}}";
            //var jsonBody = "{\"Object\":{\"Head\":{\"Fdate\":\"2025-10-22\",\"FPOStyle\":252,\"FSupplyID\":4297,\"FDCStockID\":4316,\"FSManagerID\":4300,\"FFManagerID\":4300,\"FDeptID\":\"4334\",\"FEmpID\":\"4300\",\"FExplanation\":\"测试数据\"},\"Entry\":[{\"FEntryID\":1,\"FItemID\":4375,\"FBatchNo\":\"CS1\",\"Famount\":143900,\"FAuxPrice\":143900,\"FDCSPID\":0,\"FDCStockID\":4316,\"FUnitID\":4377,\"FSecCoefficient\":0,\"FSecQty\":0,\"FMtono\":\"1\",\"Fauxqty\":1,\"FPlanMode\":14036}]}}";
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/app007104/api/purchasereceipt/create?access_token=" + access_token, dic, jsonBody);

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        [HttpPost]
        public JsonResult GetempInfo(string authdata, string access_token)
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            authdata = authdata.Replace("\\/", "/");
            string random = new RandomTool().GenerateRandom();
            var dic = new Dictionary<string, string>();
            dic.Add("KIS-Timestamp", timestamp.ToString());
            dic.Add("KIS-State", random);
            dic.Add("KIS-AuthData", authdata);
            dic.Add("KIS-TraceID", "98e813fb-9317-cbb1-db8e-a56bff07f9c2");
            dic.Add("KIS-Ver", "1.0");
            dic.Add("X-GW-Router-Addr", "https://kisyunbj.kingdee.com");
            var authService = new AuthService();
            var jsonBody = "{\"CurrentPage\":\"1\",\"ItemsOfPage\":\"100\",\"ParentId\":\"0\",\"Detail\":\"true\",\"SearchKey\":\"\"}";
            var resquest = authService.AccessUrlByPOST<Return2>("https://api.kingdee.com/koas/APP006992/api/Employee/List?access_token=" + access_token, dic, jsonBody);

            //ConcurrentLogger.Log(LogLevel.Info, resquest, "", "");
            var result = new
            {
                Message = "提交成功！",
                Code = 200
            };

            return Json(result);
        }
        public string sha256(string KISState, string session_id, string KISTraceID, string session_secret,
            string KISTimestamp)
        {
            using (SHA256 sha256 = SHA256.Create())
            {
                var input = KISState + session_id + KISTraceID + session_secret + KISTimestamp;
                byte[] bytes = Encoding.UTF8.GetBytes(input);
                byte[] hash = sha256.ComputeHash(bytes);

                StringBuilder sb = new StringBuilder();
                foreach (byte b in hash)
                {
                    sb.Append(b.ToString("x2")); // 小写十六进制，x2保证两位对齐
                }
                return sb.ToString();
            }
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                ConcurrentLogger.Stop();
            }
        }

    }
}